﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.WorldArtPrice.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.WorldArtPrice.Models;


namespace BusinessLayer.WorldArtPrice
{
    public class AdminMenuBL
    {
        private readonly DbHelper _db;

        public AdminMenuBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);
        }

        public List<AdminMenuExtend> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllAdminMenu) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<AdminMenuExtend>(oCommand);
            return list;
        }
        public int CreateAdminMenu(AdminMenu menu)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminMenu) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Code", menu.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", menu.Name));
                oCommand.Parameters.Add(new SqlParameter("@Url", menu.Url));
                oCommand.Parameters.Add(new SqlParameter("@ParentID", menu.ParentID));
                oCommand.Parameters.Add(new SqlParameter("@OrderNo", menu.OrderNo));


                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminMenu(AdminMenu menu)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateAdminMenu) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@MenuID", menu.MenuID));
                oCommand.Parameters.Add(new SqlParameter("@Code", menu.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", menu.Name));
                oCommand.Parameters.Add(new SqlParameter("@Url", menu.Url));
                oCommand.Parameters.Add(new SqlParameter("@ParentID", menu.ParentID));
                oCommand.Parameters.Add(new SqlParameter("@OrderNo", menu.OrderNo));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminMenu GetByMenuCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminMenuByCode) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Code", code));
            var list = _db.GetList<AdminMenu>(oCommand);
            return list.FirstOrDefault();
        }
        public AdminMenu GetByMenuID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminMenuByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@MenuID", id));
            var list = _db.GetList<AdminMenu>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteAdminMenuByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0, ids.Length - 1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM AdminMenu WHERE MenuID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminMenuByMenuGroupID
        public List<AdminMenu> GetAdminMenuByUserGroupID(int usergroupid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminMenuByUserGroupID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));
            var list = _db.GetList<AdminMenu>(oCommand);
            return list;
        }
        //sp_AdminMenu_Select_By_UserID
        public List<AdminMenu> GetAdminMenuByUserID(int userid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminMenuByUserID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
            var list = _db.GetList<AdminMenu>(oCommand);
            return list;
        }
        //  sp_AdminMenu_Select_By_UserName
        public List<AdminMenuExtend> GetAdminMenuByUserName(string username)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminMenuByUserName) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserName", username));
            var list = _db.GetList<AdminMenuExtend>(oCommand);
            return list;
        }
    }
}
